<template>
	<div class="wrapper">
		
		<!-- header部分 -->
		<header>
			<p>
				增添新食品
			</p>
		</header>
		
		<!-- 表单部分 -->
		<ul class="form-box">
			<li>
				<div class="title">
					食物名称：
				</div>
				<div class="content">
					<input type="text" v-model="food.foodName">
				</div>
			</li>
			<li>
				<div class="title">
					食物说明：
				</div>
				<div class="content">
					<input type="text" v-model="food.foodExplain">
				</div>
			</li>
			<li>
				<div class="title">
					食物图片：
				</div>
				<div class="content">
					<input type="text" v-model="food.foodImg">
				</div>
			</li>
			<li>
				<div class="title">
					食物价格：
				</div>
				<div class="content">
					<input type="text" v-model="food.foodPrice">
				</div>
			</li>
		</ul>
		
		<div class="button-save">
			<button @click="save()">保存</button>
		</div>
		
	</div>
</template>

<script setup>
import { ref } from 'vue';
import Footer from '../components/Footer.vue';
import axios from 'axios';
import qs from 'qs';
import { useRouter } from 'vue-router';
import { useRoute } from 'vue-router';

const route = useRoute();
const router = useRouter();
const food = ref({
  foodName: '',
  foodExplain: '',
  foodImg: '',
  foodPrice: '',
  businessId: route.query.businessId
});

const save = () => {
  if (food.value.foodName == '') {
    alert('食物名称不能为空！');
    return;
  }
  if (food.value.foodExplain == '') {
    alert('食物说明不能为空！');
    return;
  }
  if (food.value.foodImg == '') {
    alert('食物图片不能为空！');
    return;
  }
  if (food.value.foodPrice == '') {
    alert('食物价格不能为空！');
    return;
  }
  axios.post('FoodController/insertFoodInfo', qs.stringify(food.value))
    .then(response => {
      if (response.data > 0) {
        router.go(-1);
        alert('新增食物成功');
      } else {
        alert('新增食物失败！');
      }
    })
    .catch(error => {
      console.error(error);
    });
};
</script>

<style>
	/*************** 总容器 ***************/
	.wrapper {
		width: 100%;
		height: 100%;
	}
	/*************** header ***************/
	.wrapper header {
		width: 100%;
		height: 12vw;
		background-color: #0097FF;
		display: flex;
		justify-content: space-around;
		align-items: center;
		color: #fff;
		font-size: 4.8vw;
		position: fixed;
		left: 0;
		top: 0;
		/*保证在最上层*/
		z-index: 1000;
	}
	/*************** （表单信息） ***************/
	.wrapper .form-box {
		width: 100%;
		margin-top: 12vw;
	}
	
	.wrapper .form-box li {
		box-sizing: border-box;
		padding: 4vw 3vw 0vw 3vw;
		display: flex;
	}
	
	.wrapper .form-box li .title {
		flex: 0 0 18vw;
		font-size: 3vw;
		font-weight: 700;
		color: #666;
	}
	
	.wrapper .form-box li .content {
		flex: 1;
		display: flex;
		align-items: center;
	}
	
	.wrapper .form-box li .content input {
		border: none;
		outline: none;
		width: 100%;
		height: 4vw;
		font-size: 3vw;
	}
	
	.wrapper .button-add {
		box-sizing: border-box;
		padding: 4vw 3vw 0vw 3vw;
	}
	
	.wrapper .button-add button {
		width: 100%;
		height: 10vw;
		font-size: 3.8vw;
		font-weight: 700;
	
		border: none;
		outline: none;
		border-radius: 4px;
		color: #fff;
		background-color: #38CA73;
	}
	
</style>
